layui.use(['layer', 'form', 'laydate'], function() {

    var layer = layui.layer
        ,form = layui.form
        ,laydate = layui.laydate;

    laydate.render({
        elem: '.startTime'
        ,format: 'yyyy-MM-dd HH:mm:ss' //可任意组合
        ,type: 'datetime'
    });

    $(function() {

        form.on('submit(formDemo)', function(data) {
            var pId = data.field.pId
                ,pName = data.field.pName
                ,pBegintime = data.field.pBegintime
                ,pDuration = data.field.pDuration
                ,reg = /^[0-9]\d*$/;
            if ((pBegintime == "")){
                layer.tips('不能为空', '.startTime', {
                    tips: [1, '#3595CC'],
                    time: 4000
                });
                return false;
            } else {
                var now = new Date().getTime()
                    ,begintime = new Date(pBegintime).getTime();
                if (begintime <= now){
                    layer.tips('开考时间不能设置在以前', '.startTime', {
                        tips: [1, '#3595CC'],
                        time: 4000
                    });
                    return false;
                }
            }
            if (!reg.test(pDuration) || pDuration <= 0){
                layer.tips('请输入正确的考试时长', '.pDuration', {
                    tips: [1, '#3595CC'],
                    time: 4000
                });
                return false;
            }

            $.ajax({
                url:"/paper/updatePaper",
                type:"put",
                data:{
                    pId:pId,
                    pName:pName,
                    pBegintime:pBegintime,
                    pDuration:pDuration
                },
                success:function (res) {
                    if (res.code == 200){
                        //试卷修改成功
                        layer.msg(res.msg);
                        setTimeout(function () {
                            //调用父页面的方法
                            parent.tools.reloadTable();
                            //当你在iframe页面关闭自身时
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                        }, 1000);
                    } else {
                        layer.msg(res.msg);
                    }
                }
            });
            return false;
        })
    })
});